package org.command.main;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;

public class IncomeTaxCalculator {
    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create(new InetSocketAddress(3000), 0);
        server.createContext("/calculateIncomeTax", new CalculateIncomeTaxHandler());
        server.start();
    }

    static class CalculateIncomeTaxHandler implements HttpHandler {
        @Override
        public void handle(HttpExchange exchange) throws IOException {
            // 允许跨域请求
            exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
            exchange.getResponseHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            exchange.getResponseHeaders().add("Access-Control-Allow-Headers", "Content-Type");

            if ("GET".equals(exchange.getRequestMethod())) {
                // 获取查询参数中的收入
                String queryString = exchange.getRequestURI().getQuery();
                double income = Double.parseDouble(queryString.split("=")[1]);

                // 计算个人所得税（简化示例，实际应用需要更复杂的计算）
                double tax = calculateIncomeTax(income);

                // 发送响应
                String response = String.valueOf(tax);
                exchange.sendResponseHeaders(200, response.length());
                OutputStream os = exchange.getResponseBody();
                os.write(response.getBytes());
                os.close();
            } else {
                // 不支持除GET请求以外的请求方法
                exchange.sendResponseHeaders(405, 0);
                exchange.close();
            }
        }

        private double calculateIncomeTax(double income) {
            // 简化的税金计算逻辑，实际应用需要更复杂的算法
            if (income <= 10000) {
                return 0.1 * income;
            } else {
                return 0.2 * income;
            }
        }
    }
}

